﻿package mortal.game.control
{
    import Message.Public.*;
    import baseEngine.basic.*;
    import baseEngine.modifiers.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import flash.utils.*;
    import frEngine.render.layer.uiEffectLayer.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.battlefield.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class BattleFieldController extends Controller
    {
        protected var _randomT:int;
        private var isInBattleField:Boolean = false;
        private var _continueCutEffect:EffectPlayer;
        private var _continueCutEndEffect:EffectPlayer;
        private const continueCutUrl:String = "chenghao-lianzhan-1";
        private const continueCutEndUrl:String = "chenghao-zhongjielianzhan-1";
        private var _closeCutT:int;
        private var _closeEndCutT:int;

        public function BattleFieldController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldOpen, this.onBattlefieldOpen);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldClose, this.onBattlefieldClose);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldList, this.onBattlefieldList);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldBattleBegin, this.onBattlefieldBattleBegin);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldForceScore, this.onBattlefieldForceScore);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldPlayerScore, this.onBattlefieldPlayerScore);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldResult, this.onBattlefieldResult);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldContinueCut, this.showContinueCut);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldContinueCutTerminated, this.showEndContinueCut);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.BattlefieldRequstList, this.onRequstList);
            Dispatcher.addEventListener(EventName.BattlefieldRequestEnter, this.onRequestEnter);
            Dispatcher.addEventListener(EventName.BattlefieldRequestFastEnter, this.onRequestFastEnter);
            Dispatcher.addEventListener(EventName.BattlefieldRequstResult, this.onRequestResult);
            Dispatcher.addEventListener(EventName.BattlefieldShowMyScore, this.onBattlefieldShowMyScore);
            Dispatcher.addEventListener(EventName.BattlefieldGetTaskReward, this.onBattlefieldGetTaskReward);
            Dispatcher.addEventListener(EventName.BattlefieldEnterWindowShow, this.onShow);
            return;
        }// end function

        protected function onBattlefieldOpen(param1:SBattleFieldOpen) : void
        {
            if (!this.isInBattleField && cache.role.entityInfo.level >= GameConst.BattleFieldMiniEnterLevel)
            {
                BattlefieldIcon.instance.show();
            }
            return;
        }// end function

        protected function onRoleLevelUpdate(param1) : void
        {
            var _loc_2:* = cache.battlefieldCache.battlefieldOpen;
            if (_loc_2 && !this.isInBattleField && cache.role.entityInfo.level >= GameConst.BattleFieldMiniEnterLevel)
            {
                BattlefieldIcon.instance.show();
            }
            return;
        }// end function

        protected function onBattlefieldClose(param1) : void
        {
            if (BattlefieldIcon.hasInstance())
            {
                BattlefieldIcon.instance.hide();
            }
            if (BattlefieldEnterWindowNew.hasInstance())
            {
                BattlefieldEnterWindowNew.instance.hide();
            }
            return;
        }// end function

        protected function onBattlefieldList(param1) : void
        {
            if (!this.isInBattleField && cache.role.entityInfo.level >= GameConst.BattleFieldMiniEnterLevel)
            {
                BattlefieldEnterWindowNew.instance.show();
            }
            return;
        }// end function

        protected function onBattlefieldBattleBegin(param1) : void
        {
            if (!this.isInBattleField && cache.role.entityInfo.level >= GameConst.BattleFieldMiniEnterLevel)
            {
                this.randomTimeShow();
            }
            return;
        }// end function

        protected function randomTimeShow() : void
        {
            this._randomT = MathUitl.random(0, 10 * 1000);
            setTimeout(this.showEnterWindow, this._randomT);
            BattlefieldEnterWindowNew.instance.addEventListener(WindowEvent.SHOW, this.onEnterWindowShow);
            return;
        }// end function

        protected function onEnterWindowShow(event:WindowEvent) : void
        {
            BattlefieldEnterWindowNew.instance.removeEventListener(WindowEvent.SHOW, this.onEnterWindowShow);
            if (this._randomT)
            {
                clearTimeout(this._randomT);
                this._randomT = 0;
            }
            return;
        }// end function

        protected function showEnterWindow() : void
        {
            if (!this.isInBattleField && cache.role.entityInfo.level >= GameConst.BattleFieldMiniEnterLevel)
            {
                BattlefieldEnterWindowNew.instance.show();
                BattlefieldEnterWindowNew.instance.updateIsOpen(true);
            }
            return;
        }// end function

        protected function onBattlefieldForceScore(param1) : void
        {
            if (BattlefieldMiniView.hasInstance() && !BattlefieldMiniView.instance.isHide)
            {
                BattlefieldMiniView.instance.updateForceScore(cache.battlefieldCache.battlefieldScoreList);
            }
            return;
        }// end function

        protected function onBattlefieldPlayerScore(param1) : void
        {
            if (BattlefieldMiniView.hasInstance() && !BattlefieldMiniView.instance.isHide)
            {
                BattlefieldMiniView.instance.updatePlayerScore(cache.battlefieldCache.myScore);
            }
            return;
        }// end function

        protected function onBattlefieldResult(param1) : void
        {
            BattlefieldResultWindow.instance.show();
            BattlefieldResultWindow.instance.updateScore();
            return;
        }// end function

        protected function onSceneUpdate(event:DataEvent) : void
        {
            if (cache.role.entityInfo.level < GameConst.BattleFieldMiniEnterLevel)
            {
                return;
            }
            if (!this.isInBattleField && GameMapUtil.curMapState.isBattlefield)
            {
                this.isInBattleField = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
                if (BattlefieldIcon.hasInstance())
                {
                    BattlefieldIcon.instance.hide();
                }
                if (BattlefieldEnterWindowNew.hasInstance())
                {
                    BattlefieldEnterWindowNew.instance.hide();
                }
                BattlefieldMiniView.instance.show();
                BattlefieldMiniView.instance.updateAll();
                AIManager.cancelAll();
            }
            if (this.isInBattleField && !GameMapUtil.curMapState.isBattlefield)
            {
                this.isInBattleField = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
                if (BattlefieldMiniView.hasInstance())
                {
                    BattlefieldMiniView.instance.hide();
                }
                if (cache.battlefieldCache.isOpen)
                {
                    BattlefieldIcon.instance.show();
                }
            }
            return;
        }// end function

        private function onRequstList(event:DataEvent) : void
        {
            if (cache.battlefieldCache.isOpen)
            {
                GameProxy.battlefield.getBattlefieldList();
            }
            return;
        }// end function

        private function onRequestEnter(event:DataEvent) : void
        {
            if (cache.role.entityInfo.level < GameConst.BattleFieldMiniEnterLevel)
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20384, GameConst.BattleFieldMiniEnterLevel));
                return;
            }
            var _loc_2:* = event.data as int;
            GameProxy.battlefield.enterBattlefield(_loc_2);
            return;
        }// end function

        private function onRequestFastEnter(event:DataEvent) : void
        {
            if (cache.role.entityInfo.level < GameConst.BattleFieldMiniEnterLevel)
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20384, GameConst.BattleFieldMiniEnterLevel));
                return;
            }
            GameProxy.battlefield.enterBattlefield(1);
            return;
        }// end function

        private function onRequestResult(event:DataEvent) : void
        {
            GameProxy.battlefield.getBattlefieldResult();
            return;
        }// end function

        private function onBattlefieldShowMyScore(event:DataEvent) : void
        {
            GameProxy.battlefield.showScore();
            return;
        }// end function

        private function onBattlefieldGetTaskReward(event:DataEvent) : void
        {
            GameProxy.battlefield.getTaskReward();
            return;
        }// end function

        private function onShow(event:DataEvent) : void
        {
            if (BattlefieldIcon.hasInstance() && !BattlefieldIcon.instance.isHide)
            {
                if (BattlefieldEnterWindowNew.instance.isHide)
                {
                    BattlefieldEnterWindowNew.instance.show();
                    BattlefieldEnterWindowNew.instance.updateBeginDt(cache.battlefieldCache.battlefieldOpen.openDt);
                }
                else
                {
                    BattlefieldEnterWindowNew.instance.hide();
                }
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(20385));
            }
            return;
        }// end function

        public function showContinueCut(param1:int) : void
        {
            if (param1 <= 1)
            {
                return;
            }
            var _loc_2:* = UIEffectList.instance.getRenderListByUILayerID(UILayerIDs.ScreenID);
            if (this._continueCutEffect)
            {
                this._continueCutEffect.dispose();
                this._continueCutEffect = null;
            }
            this._continueCutEffect = EffectPlayerPool.instance.getEffectPlayer(this.continueCutUrl, Game.scene.playerLayer, _loc_2, true);
            this._continueCutEffect.x = -250;
            this._continueCutEffect.y = 100;
            this._continueCutEffect.play(PlayMode.ANIMATION_LOOP_MODE);
            BattlefieldContinueCutNumView.instance.show();
            BattlefieldContinueCutNumView.instance.updateNum(param1);
            this.addTimeCloseContinueCut();
            return;
        }// end function

        private function closeContinueCut() : void
        {
            this._continueCutEffect.dispose();
            this._continueCutEffect = null;
            BattlefieldContinueCutNumView.instance.hide();
            this.clearTimeCloseContinueCut();
            return;
        }// end function

        private function addTimeCloseContinueCut() : void
        {
            this.clearTimeCloseContinueCut();
            this._closeCutT = setTimeout(this.closeContinueCut, 5000);
            return;
        }// end function

        private function clearTimeCloseContinueCut() : void
        {
            if (this._closeCutT)
            {
                clearTimeout(this._closeCutT);
                this._closeCutT = 0;
            }
            return;
        }// end function

        public function showEndContinueCut(param1 = null) : void
        {
            var _loc_2:* = UIEffectList.instance.getRenderListByUILayerID(UILayerIDs.ScreenID);
            if (this._continueCutEndEffect)
            {
                this._continueCutEndEffect.dispose();
                this._continueCutEndEffect = null;
            }
            this._continueCutEndEffect = EffectPlayerPool.instance.getEffectPlayer(this.continueCutEndUrl, Game.scene.playerLayer, _loc_2, true);
            this._continueCutEndEffect.x = -250;
            this._continueCutEndEffect.y = 200;
            this._continueCutEndEffect.play(PlayMode.ANIMATION_LOOP_MODE);
            this.addTimeEndCloseContinueCut();
            return;
        }// end function

        private function closeEndContinueCut() : void
        {
            this._continueCutEndEffect.dispose();
            this._continueCutEndEffect = null;
            this.clearTimeEndCloseContinueCut();
            return;
        }// end function

        private function addTimeEndCloseContinueCut() : void
        {
            this.clearTimeEndCloseContinueCut();
            this._closeEndCutT = setTimeout(this.closeEndContinueCut, 3000);
            return;
        }// end function

        private function clearTimeEndCloseContinueCut() : void
        {
            if (this._closeEndCutT)
            {
                clearTimeout(this._closeEndCutT);
                this._closeEndCutT = 0;
            }
            return;
        }// end function

    }
}
